The Bizarre Hidden Powers of the Preprocessor? [closed]

Posted by ApprenticeHacker on Stack Overflow See other posts from Stack Overflow or by ApprenticeHacker
Published on 2012-04-04T05:42:36Z Indexed on 2012/10/02 21:37 UTC
Read the original article Hit count: 443

Filed under:
|

The preprocessor in C and C++ deserves an entire essay on its own to explore its rich possibilities for obfuscation.

It is true that the C++ (and C) preprocessor can be used for a lot of powerful stuff. #ifdefs and #defines are often used to determine platforms, compilers and backends. Manipulating the code likewise. However, can anyone list some of the most powerful and bizarre things you can do with the preprocessor?

The most sinister use of the preprocessor I've found is this:

#ifndef DONE 

#ifdef TWICE 

// put stuff here to declare 3rd time around 
void g(char* str); 
#define DONE 

#else // TWICE 
#ifdef ONCE 

// put stuff here to declare 2nd time around 
void g(void* str); 
#define TWICE 

#else // ONCE 

// put stuff here to declare 1st time around 
void g(std::string str); 
#define ONCE 

#endif // ONCE 
#endif // TWICE 
#endif // DONE

This declares different things based on how many times the header is included.


Are there any other bizarre unknown powers of the C++ preprocessor?

© Stack Overflow or respective owner

Related posts about c++

Related posts about preprocessor